<?php

namespace App\Http\Controllers\Commodity;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Commodity_classify;
use App\Model\Commodity;
use App\Model\Commodity_color;
use App\Model\Commodity_size;
use App\Model\Commodity_info;
use Illuminate\Support\Facades\DB;

/**
 * Class CommodityController
 * @package App\Http\Controllers\Commodity
 * @User wuqin
 * @Date 2018/3/29 0029
 * @Time 20:05
 */
class CommodityController extends Controller
{
    public function res(Request $request, string $name)
    {
        $user = session('user_info');
        if (empty($user))
            return $this->error_url('/', '请重新登录', 3);

        if (empty($name))
            return $this->error_url('/', '请重新登录', 3);

        //获取该分类商品
        $commodityModel = new Commodity();
        $commodity = $commodityModel->get_commodity_by_name($name);
        if (!empty($commodity)) {
            $infoModel = new Commodity_info();
            foreach ($commodity as $key => $value) {
                $keys = $infoModel->get_info_keys_by_commodity((int)$value['commodity_id']);
                preg_match('/(?<=commodity_info:).*?(?=(:commodity))/', $keys[0], $matches);
                if (empty($matches) || $matches[0] == '0') {
                    unset($commodity[$key]);
                    continue;
                }
                $res = $infoModel->mget_array_redis($keys);
                if (empty($res) || count($res) == 0)
                    unset($commodity[$key]);
                $commodity[$key]['commodity_info_price'] = reset($res)['commodity_info_price'];
            }
        }
        $data["commodity"] = $commodity;
        $data["keyword"] = $name;
        return view("Commodity.res", $data);
    }
    /**
     * 方法 : commodity
     * @User wuqin
     * @DateTime 2018-03-29 20:05
     * @param Request $request
     * @param $commodityId
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function commodity(Request $request, int $commodityId)
    {
        $user = session('user_info');
        if (empty($user))
            return $this->error_url('/', '请重新登录', 3);
        $commodityModel = new Commodity();
        $sizeModel = new Commodity_size();
        $colorModel = new Commodity_color();
        $infoModel = new Commodity_info();

        $info = $infoModel->get_info_by_commodity_id($commodityId);
        $size = $sizeModel->get_size_by_commodity_id($commodityId);
        $color = $colorModel->get_color_by_commodity_id($commodityId);

        if(!empty($info)){
            foreach ($info as $key => $value) {
                $resSize['s'.$value['commodity_size_id']][] = $value['commodity_color_id'];
                $resColor['c'.$value['commodity_color_id']][] = $value['commodity_size_id'];
                $res['v'.$value['commodity_color_id'].'_'.$value['commodity_size_id']] = $value;
            }
            $data['info'] = $res;
            $data['resSize'] = $resSize;
            $data['resColor'] = $resColor;
        }
        $data['size'] = $size;
        $data['color'] = $color;
        $data['commodity'] = $commodityModel->get_commodity_by_commidity_id($commodityId);
        $data['commodity'] = reset($data['commodity']);
        $data['user'] = session("wechat.oauth_user.default")->toArray();
        return view('Commodity.commodity',$data);
    }

    /**
     * 方法 : list
     * @User wuqin
     * @DateTime 2018-03-29 20:05
     * @param Request $request
     * @param $parentId
     * @param $classifyId
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function list(Request $request, $parentId, $classifyId)
    {
        $user = session('user_info');
        if (empty($user))
            return $this->error_url('/', '请重新登录', 3);
        //获取分类
        $classifyModel = new Commodity_classify();
        $classify = $classifyModel->get_classify($parentId);
        $last_col = $classify[$classifyId];
        unset($classify[$classifyId]);
        array_unshift($classify, $last_col);

        //获取该分类商品
//        $commodityModel = new Commodity();
//        $commodity = $commodityModel->get_commodity_by_classify_id((int)$classifyId);
//
//        if (!empty($commodity)) {
//            $infoModel = new Commodity_info();
//            foreach ($commodity as $key => $value) {
//                $keys = $infoModel->get_info_keys_by_commodity((int)$value['commodity_id']);
//                preg_match('/(?<=commodity_info:).*?(?=(:commodity))/', $keys[0], $matches);
//                if ($matches[0] == '0') {
//                    unset($commodity[$key]);
//                    continue;
//                }
//                $res = $infoModel->mget_array_redis($keys);
//                if (empty($res))
//                    unset($commodity[$key]);
//            }
//        }

        return view('Commodity.list', array('classify' => $classify));
    }

    /**
     * 方法 : search
     * @User wuqin
     * @DateTime 2018-03-29 20:05
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function search(Request $request)
    {
        $user = session('user_info');
        if (empty($user))
            return $this->error_url('/', '请重新登录', 3);
        return view('Commodity.search');
    }

    /**
     * 方法 : do_search
     * @User wuqin
     * @DateTime 2018-03-29 20:05
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function do_search(Request $request)
    {
        $user = session('user_info');
        if (empty($user))
            return $this->error_url('/', '请重新登录', 3);
        $data = $request->input();
        if (empty($data))
            return $this->error_url('/', '非法操作', 3);
        return view('Commodity.search');
    }
}
